import { FileTransfer, FileTransferObject } from '@ionic-native/file-transfer/ngx';
import { ImagePicker } from '@ionic-native/image-picker/ngx';
import { Component, OnInit } from '@angular/core';
import { ApiService } from '../api/api.service';
import { ApiUrl } from '../api/ApiUrl';
import { AnswerVO } from '../vo/Answervo';
import { Router } from '@angular/router';
import { CompanyVO } from '../vo/CompanyVO';
import { ToastController } from '@ionic/angular';
import { UserVO } from '../vo/UserVO';
@Component({
  selector: 'app-addvisit',
  templateUrl: './addvisit.page.html',
  styleUrls: ['./addvisit.page.scss'],
})
export class AddvisitPage implements OnInit {
  orgitems: CompanyVO[] = [];
  orgitem: CompanyVO;
  ccompanylinkname: string;
  ccompanylinkphone: string;
  iorgname: string;
  cbankcode: string;
  dvisitdate = '';
  icompanytype: string;
  inature: string;
  cnow: string;
  cadd: string;
  isolve: string;
  creason: string;
  clast: string;
  item: AnswerVO;
  userItem: UserVO;
  corgname: string;
  url: any;
  // 默认图片
  avatarPath = 'assets/image/avatar.png';
  data: string;
  imageBase64: Array<string> = [];

  constructor(private toastController: ToastController, public api: ApiService, private router: Router,
    private imagePicker: ImagePicker, private transfer: FileTransfer) { }
  fileTransfer: FileTransferObject = this.transfer.create();

  ngOnInit() {
    this.getOrgItems();
    this.userItem = ApiUrl.user;
    console.log(111);
  }
  // 获取单位列表
  getOrgItems() {
    this.api.getItem<AnswerVO>(ApiUrl.getOrgItems, { 'itype': 3 }).subscribe(response => {
      this.orgitems = <CompanyVO[]>response.data;
    }, () => {
      console.log('出错了');
    });
  }

  iorgnameChange() {
    this.api.getItem<CompanyVO>(ApiUrl.findOneById, { 'id': this.iorgname }).subscribe(response => {
      this.orgitem = response;
      this.ccompanylinkname = this.orgitem.clinkman;
      this.ccompanylinkphone = this.orgitem.cphone;
      console.log(this.iorgname);
      console.log(this.orgitem);
    }, () => {
      console.log('出错了');
    });
  }

  checkSave() {
    if (this.dvisitdate.length === 0) {
      this.presentToast('请选择走访日期');
      return;
    }
    console.log(this.dvisitdate);
    if (this.iorgname.length === 0) {
      this.presentToast('请选择企业名称');
      return;
    }
    if (this.cbankcode.length === 0) {
      this.presentToast('请填写组织机构代码');
      return;
    }
    if (this.icompanytype.length === 0) {
      this.presentToast('请选择企业类型');
      return;
    }
    if (this.ccompanylinkname.length === 0) {
      this.presentToast('请填写企业联系人');
      return;
    }
    if (this.ccompanylinkphone.length === 0) {
      this.presentToast('请填写企业联系方式');
      return;
    }
    if (this.inature.length === 0) {
      this.presentToast('请选择客户性质');
      return;
    }
    if (this.cnow.length === 0) {
      this.presentToast('请填写企业经营及融资情况');
      return;
    }
    if (this.cadd.length === 0) {
      this.presentToast('请填写融资需求和存在困难');
      return;
    }
    if (this.isolve.length === 0) {
      this.presentToast('请选择融资需求和存在困难能否解决');
      return;
    }
    if (this.creason.length === 0) {
      this.presentToast('请填写解决方案');
      return;
    }
    if (this.clast.length === 0) {
      this.presentToast('请填写融资需求最终解决情况');
      return;
    }
    this.save();
  }
  save() {
    this.api.getItem<AnswerVO>(ApiUrl.toSaveVisit, {
      'icompany': this.iorgname,
      'cbankcode': this.cbankcode, 'dvisitdate': this.dvisitdate.substring(0, 10),
      'icompanytype': this.icompanytype, 'iconsultant': this.userItem.iconsultantid,
      'inature': this.inature, 'cnow': this.cnow, 'cadd': this.cadd, 'isolve': this.isolve, 'creason': this.creason, 'clast': this.clast,
      'base64': this.imageBase64
    }).subscribe(response => {

      this.item = response;
      if (this.item.success) {
        this.presentToast(this.item.msg);
      } else {
        this.presentToast(this.item.msg);
      }
      this.router.navigate(['/tabs']);
    }, () => {
      console.log('出错了');
    });
  }
  async presentToast(msg) {
    const toast = await this.toastController.create({
      message: msg,
      duration: 2000
    });
    toast.present();
  }


  getPicture() {
    this.data = '';
    // this.imageBase64 = [];

    // options 里的具体内容请参照官网https://ionicframework.com/docs/native/image-picker/
    const options = {
      maximumImagesCount: 5,
      outputType: 1,
      width: 400,
      height: 400,
      quality: 50
    };
    this.imagePicker.getPictures(options).then((results) => {
      console.log(results);
      for (let i = 0; i < results.length; i++) {
        console.log('Image URI: ' + results[i]);
        // 保存图片到html控件
        const imgUrl = '<img src=data:image/png;base64,' + results[i] + ' width="60px" height="60px">  ';
        this.data = this.data + imgUrl;
        this.imageBase64.push((<string>results[i]).replace(/\+/g, '_'));
      }
    }, (err) => {
      alert('error');
    });
  }
}
